import request from '@/utils/request'
import message from 'ant-design-vue/es/message'
import modal from 'ant-design-vue/es/modal'
import { SOURCE_API_DOMAIN } from '@/store/mutation-types'

export default {
    // 获取列表
    fetchList (url, params) {
        return new Promise((resolve, reject) => {
            request({
                url: url,
                method: 'get',
                params: {
                    ...params
                },
                type: 'json'
            }).then(data => {
                data.message = data.msg
                resolve(data)
            }).catch(error => {
                reject(error)
            })
        })
    },
    // 获取信息详情
    getInfo (url) {
        return new Promise((resolve, reject) => {
            request({
                url: url,
                method: 'get',
                type: 'json'
            }).then(data => {
                data.message = data.msg
                resolve(data)
            }).catch(error => {
                reject(error)
            })
        })
    },
    // 通过get获取信息
    handleGet (url, params) {
        return new Promise((resolve, reject) => {
            request({
                url: url,
                method: 'get',
                params: {
                    ...params
                },
                type: 'json'
            }).then(data => {
                data.message = data.msg
                resolve(data)
            }).catch(error => {
                reject(error)
            })
        })
    },
    // 通过post获取信息
    handlePost (url, params) {
        return new Promise((resolve, reject) => {
            request({
                url: url,
                method: 'post',
                data: {
                    ...params
                },
                type: 'json'
            }).then(data => {
                if (data.msg) data.message = data.msg
                resolve(data)
            }).catch(error => {
                reject(error)
            })
        })
    },
    // 改变某些字段值
    changeField (url, params) {
        return new Promise((resolve, reject) => {
            request({
                url: url,
                method: 'post',
                data: params,
                type: 'json'
            }).then(data => {
                data.message = data.msg
                resolve(data)
            }).catch(error => {
                reject(error)
            })
        })
    },
    // 删除数据 - get(一条)
    deleteOne (url, params, id) {
        return new Promise((resolve, reject) => {
            if (id) {
                modal.confirm({
                    title: '确定要删除吗?',
                    onOk () {
                        request({
                            url: url,
                            method: 'post',
                            data: params,
                            type: 'json'
                        }).then(data => {
                            if (data.code === 200) {
                                message.success('删除成功')
                                resolve(true)
                            } else {
                                message.error(data.msg)
                                resolve(false)
                            }
                        }).catch(error => {
                            reject(error)
                        })
                    },
                    onCancel () {
                        console.log('Cancel')
                    },
                    class: 'test'
                })
            } else {
                message.error('请选择要删除的内容')
            }
        })
    },
     // 删除数据 - get(一条)
     deleteOnePlus (url, id) {
        return new Promise((resolve, reject) => {
            if (id) {
                modal.confirm({
                    title: '确定要删除吗?',
                    onOk () {
                        request({
                            url: url,
                            method: 'get',
                            type: 'json'
                        }).then(data => {
                            if (data.code === 200) {
                                message.success('删除成功')
                                resolve(true)
                            } else {
                                message.error(data.msg)
                                resolve(false)
                            }
                        }).catch(error => {
                            reject(error)
                        })
                    },
                    onCancel () {
                        console.log('Cancel')
                    },
                    class: 'test'
                })
            } else {
                message.error('请选择要删除的内容')
            }
        })
    },
    // 删除数据 - post(一条或多条)
    delete (url, params, deleteIds) {
        return new Promise((resolve, reject) => {
            if (deleteIds.length > 0) {
                modal.confirm({
                    title: '确定要删除吗?',
                    onOk () {
                        request({
                            url: url,
                            method: 'post',
                            type: 'json',
                            data: params
                        }).then(data => {
                            if (data.code === 200) {
                                message.success('删除成功')
                                resolve(true)
                            } else {
                                message.error(data.msg)
                                resolve(false)
                            }
                        }).catch(error => {
                            reject(error)
                        })
                    },
                    onCancel () {
                        console.log('Cancel')
                    },
                    class: 'test'
                })
            } else {
                message.error('请选择要删除的内容')
            }
        })
    },
    // 导入excel
    importExcel (url, formData) {
        return new Promise((resolve, reject) => {
            request({
                url: SOURCE_API_DOMAIN + url,
                method: 'post',
                data: formData
            }).then(data => {
                resolve(data)
            }).catch(error => {
                reject(error)
            })
        })
    },
    // 导出excel
    exportExcel (url, formData) {
        return new Promise((resolve, reject) => {
            request({
                url: url,
                method: 'post',
                data: formData,
                'responseType': 'arraybuffer'
            }).then(data => {
                resolve(data)
            }).catch(error => {
                reject(error)
            })
        })
    },
    // 上传图片
    uploadImageFile (formData) {
        return new Promise((resolve, reject) => {
            request({
                url: SOURCE_API_DOMAIN + '/image/upload',
                method: 'post',
                data: formData
            }).then(data => {
                resolve(data)
            }).catch(error => {
                reject(error)
            })
        })
    },
    // 上传视频
    uploadVideoFile (formData) {
        return new Promise((resolve, reject) => {
            request({
                url: SOURCE_API_DOMAIN + '/obs/uploadObjectOSS',
                method: 'post',
                data: formData
            }).then(data => {
                resolve(data)
            }).catch(error => {
                reject(error)
            })
        })
    },
    // 上传附件
    uploadFile (formData) {
        return new Promise((resolve, reject) => {
            request({
                url: SOURCE_API_DOMAIN + '/file/upload',
                method: 'post',
                data: formData
            }).then(data => {
                resolve(data)
            }).catch(error => {
                reject(error)
            })
        })
    },
    // 上传音频
    uploadAudioFile (formData) {
        return new Promise((resolve, reject) => {
            request({
                url: '/file/audio/upload',
                method: 'post',
                data: formData
            }).then(data => {
                resolve(data)
            }).catch(error => {
                reject(error)
            })
        })
    },
    /**
     * 组建 a-tree-select
     * 树选择框,构造转换，转换后端数据为树形选择需要的数据
     * @param data  后端数据
     * @param name  后端数据 - 显示的文字字段名称
     * @param id    后端数据 - 主键字段名称
     * @returns {[]}  返回结构
     */
    convertToTreedata (data, name, id) {
        // console.log(data, '数据构造........')
        var returnData = []
        // 遍历数据
        for (var i = 0; i < data.length; i++) {
            var tempObj = {
                title: data[i][name],
                value: data[i][id],
                key: data[i][id]
            }
            // 判断是否存在子节点，如果存在则递归
            if ('children' in data[i]) {
                tempObj.children = this.convertToTreedata(data[i].children)
            }
            // push到数据数组中
            returnData.push(tempObj)
        }
        return returnData
    }
}
